package com.geek.bams.utils;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
 *@Date 2025/11/9
 *@Author wangpeng
 *@Description TODO
 */
public class ThreadPool {

    private ThreadPoolExecutor pool;
    /**
     * 创建线程池
     * @param maxPoolSize  池子中允许最大的线程数量
     * @param queueSize    阻塞队列中允许的线程数
     * 自定义线程池，执行的最多任务数 = maxPoolSize + queueSize
     */
    public ThreadPool(int maxPoolSize,int queueSize){
        pool = new ThreadPoolExecutor(
                20,maxPoolSize,
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(queueSize),
                new ThreadPoolExecutor.DiscardOldestPolicy());
    }
    public void exec(Runnable runnable){
        //pool.execute(runnable);  //直接执行任务，无返回值
        pool.submit(runnable);   //提交任务并返回Future对象，可用于获取执行结果或控制任务
    }
}
